add<template>
  <div class="app-container">
    <el-form :inline="true" label-width="68px">
      <el-form-item label="内容主题">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入内容主题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属栏目">
        <el-select v-model="queryParams.categoryId" placeholder="所属栏目" clearable size="small">
          <el-option
            v-for="dict in categoryList"
            :key="dict.categoryId"
            :label="dict.name"
            :value="dict.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="articleList">
      <el-table-column label="内容主题" align="center" prop="title" width="300"/>
      <el-table-column label="所属栏目" align="center" prop="categoryId" :formatter="categoryFormat"/>
      <el-table-column label="是否允许评论" align="center" prop="allowcomments" :formatter="allowcommentsFormat" />
      <el-table-column label="阅读数量" align="center" prop="readnumber" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="text" 
            icon="el-icon-edit" 
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="queryParams.pageSize"
            :page-sizes="[5,10,15]"
            :current-page.sync="queryParams.pageNum"
            :total="total">
          </el-pagination>
    </div>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle,listCategory } from "@/api/cms/article"

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      articleList: [],
      categoryList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [{dictValue:"0",dictLabel:"正常"},{dictValue:"1",dictLabel:"停用"}],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        categoryId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "内容主题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "内容分类不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCategory();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getCategory() {
        listCategory().then(response => {
          this.categoryList = response.data;
        })
      },
    categoryFormat(row)
      {
        
        var categoryId=row.categoryId;
        var actions = [];
        Object.keys(this.categoryList).map((key) => {
          if (this.categoryList[key].categoryId == ('' + categoryId)) {
            actions.push(this.categoryList[key].name);
            return false;
          }
        })
        return actions.join('');
      },
    allowcommentsFormat(row, column) {
      return row.allowcomments === "0" ? "是" : "否";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path:'/cms/addarticle'});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path:'/cms/addarticle',query:{id:row.articleId}});
    },
    
    handleSizeChange(val) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = val;
        this.getList();
      },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除主题为"' + row.title + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delArticle(row.articleId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>